﻿using SIR.Common;
using SIR.Common.Condition;
using SIR.Entities.Interfaces;
using System.Linq;

namespace SIR.Entities.Implementations
{
	/// <summary>
	/// Represents an image that is waiting to be processes by the analysis service
	/// </summary>
	public class QueuedImage : Entity, IQueuedImage
	{
		/// <summary>
		/// Gets or sets the identifier for the image
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public long ImageId { get; set; }
		/// <summary>
		/// Gets or sets the processing status
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.None)]
		public ProcessingStatus Status { get; set; }
		/// <summary>
		/// Gets or sets the identifier associated to the processing status
		/// </summary>
		[Newtonsoft.Json.JsonIgnore]
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public int StatusId
		{
			get { return (int)Status; }
			set { Status = (ProcessingStatus)value; }
		}
		/// <summary>
		/// Gets the priority of the image in the queue
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.None)]
		public ProcessingPriority Priority { get; set; }
		/// <summary>
		/// Gets or sets the priority identifier associtated to the processing priority
		/// </summary>
		[Newtonsoft.Json.JsonIgnore]
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public int PriorityId
		{
			get { return (int)Priority; }
			set { Priority = (ProcessingPriority)value; }
		}

		private IImage _image;
		/// <summary>
		/// Gets the image reference by the place in the queue
		/// </summary>
		[Newtonsoft.Json.JsonIgnore]
		[DatabaseAccess(DatabaseAccessRights.None)]
		public IImage Image
		{
			get
			{
				if (_image == null)
					_image = DAL.DataServer.Find<Image>(new Predicate("Id", ImageId, PredicateOperator.EqualTo)).Results.FirstOrDefault();
				return _image;
			}
		}
	}
}
